package com.xiaowu.netmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowu.netmusic.domain.po.CommentLike;
import com.xiaowu.netmusic.mapper.CommentLikeMapper;
import com.xiaowu.netmusic.service.CommentLikeService;
import org.springframework.stereotype.Service;

@Service
public class CommentLikeServiceImpl extends ServiceImpl<CommentLikeMapper, CommentLike>
        implements CommentLikeService {

    @Override
    public boolean likeComment(Long userId, Long commentId) {
        // 若已点赞，不重复插入
        QueryWrapper<CommentLike> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("comment_id", commentId);
        if (this.count(wrapper) > 0) {
            return true;
        }

        CommentLike like = new CommentLike();
        like.setUserId(userId);
        like.setCommentId(commentId);
        return this.save(like);
    }

    @Override
    public boolean cancelLikeComment(Long userId, Long commentId) {
        return this.remove(
                new QueryWrapper<CommentLike>().eq("user_id", userId).eq("comment_id", commentId));
    }

    @Override
    public boolean isCommentLiked(Long userId, Long commentId) {
        return this.count(
                new QueryWrapper<CommentLike>().eq("user_id", userId).eq("comment_id", commentId)) > 0;
    }
}
